package org.cytoscape.myapp.cytoscape_app_v1.internal.UI;

import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter.DEFAULT;

import org.cytoscape.event.CyEventHelper;
import org.cytoscape.myapp.cytoscape_app_v1.internal.model.InfoVisualOption;
import org.cytoscape.myapp.cytoscape_app_v1.internal.model.NetworkSettings;
import org.cytoscape.work.TaskManager;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTextField;

public class VisualLayoutOption extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private NetworkSettings networkSettings;
	private TaskManager taskManager;
	private CyEventHelper eventHelper;	
	
	public static String[] options = NetworkSettings.selectVisualOption;
	
	private JComboBox<String> comboBoxWidth;
	private JComboBox<String> comboBoxColor;
	private JComboBox<String> comboBoxLenght;
	
	private JTextField textField;
	private JTextField textField_1;



	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					VisualLayoutOption frame = new VisualLayoutOption(null,null,null);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * @param taskManager 
	 * @param currentNetworkSUID 
	 * @param utilNetworkManager 
	 */
	public VisualLayoutOption(NetworkSettings networkSettingss, final TaskManager taskManager,final CyEventHelper eventHelper) {
		this.networkSettings = networkSettingss;
		this.taskManager = taskManager;
		this.eventHelper = eventHelper;
		setTitle("Visual Options");
		setBounds(100, 100, 300, 330);
		contentPane = new JPanel();
		contentPane.setToolTipText("");
		contentPane.setBorder(null);
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		this.comboBoxWidth = new JComboBox();
		comboBoxWidth.setBounds(116, 51, 138, 20);
		contentPane.add(comboBoxWidth);
		
		
		this.comboBoxColor = new JComboBox();
		comboBoxColor.setBounds(116, 82, 138, 20);
		contentPane.add(comboBoxColor);
	
		
		this.comboBoxLenght = new JComboBox();
		comboBoxLenght.setBounds(116, 113, 138, 20);
		contentPane.add(comboBoxLenght);
		
		
		JLabel lblNewLabel = new JLabel("Lenght Edge");
		lblNewLabel.setBounds(26, 116, 71, 14);
		contentPane.add(lblNewLabel);
		
		JLabel lblWid = new JLabel("Width Edge");
		lblWid.setBounds(26, 54, 62, 14);
		contentPane.add(lblWid);
		
		JLabel lblColorEdge = new JLabel("Color Edge");
		lblColorEdge.setBounds(26, 85, 62, 14);
		contentPane.add(lblColorEdge);
		
		JButton btnDoLayout = new JButton("Do Layout");
		btnDoLayout.setBounds(164, 256, 116, 23);
		contentPane.add(btnDoLayout);
		
		btnDoLayout.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {
				networkSettings.setVisualOptions(getInfoVisualOption());
				taskManager.execute(networkSettings.getTaskIteratorVisualStyle());
				eventHelper.flushPayloadEvents();
			}
		});
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Visual Properties", TitledBorder.LEFT, TitledBorder.TOP, null, null));
		panel.setBounds(10, 11, 270, 148);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JPanel panel_1 = new JPanel();
		panel_1.setLayout(null);
		panel_1.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Layout Options", TitledBorder.LEFT, TitledBorder.TOP, null, null));
		panel_1.setBounds(10, 180, 270, 65);
		contentPane.add(panel_1);
		
		textField_1 = new JTextField();
		textField_1.setColumns(10);
		textField_1.setBounds(112, 138, 86, 20);
		panel_1.add(textField_1);
		
		textField = new JTextField();
		textField.setToolTipText("Amount Iterations");
		textField.setText("100");
		textField.setBounds(82, 28, 86, 20);
		panel_1.add(textField);
		textField.setColumns(10);
		
		JLabel lblIterations = new JLabel("Iterations");
		lblIterations.setBounds(10, 31, 62, 14);
		panel_1.add(lblIterations);
		panel_1.setVisible(true);
		
		
		this.loadCombobox();
	}
	
	public InfoVisualOption getInfoVisualOption(){
		 InfoVisualOption visualInfo = new InfoVisualOption();
		 visualInfo.setEdgeWith((String)this.comboBoxWidth.getSelectedItem());
		 visualInfo.setEdgeColor((String)this.comboBoxColor.getSelectedItem());
		 visualInfo.setEdgeLenght((String)this.comboBoxLenght.getSelectedItem());
		 visualInfo.setIterations(Integer.valueOf(this.textField.getText()));
		 return visualInfo;
	}
	
	private void loadCombobox(){
		
		for (String value : this.options) {
			this.comboBoxWidth.addItem(value);
			this.comboBoxColor.addItem(value);
			this.comboBoxLenght.addItem(value);
		}
		
		this.comboBoxWidth.setSelectedIndex(0);
		this.comboBoxColor.setSelectedIndex(1);
		this.comboBoxLenght.setSelectedIndex(2);
		
	}
	
	public VisualLayoutOption() {
		// TODO Auto-generated constructor stub
	}
	
}
